package cao.oschina.bean;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 引用
 * <p/>
 * Created by Haidy on 15/4/1.
 */
public class Refer {

    public String refertitle;
    public String referbody;

    public static List<Refer> parser(NodeList nodeList) {
        List<Refer> referList = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            NodeList childNodes = nodeList.item(i).getChildNodes();
            Refer refer = new Refer();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node childNode = childNodes.item(j);
                switch (childNode.getNodeName()) {
                    case "refertitle":
                        refer.refertitle = childNode.getFirstChild().getNodeValue();
                        break;
                    case "referbody":
                        refer.referbody = childNode.getFirstChild().getNodeValue();
                        break;
                }
            }
            if (refer.referbody != null && refer.refertitle != null) {
                referList.add(refer);
            }
        }
        return referList;
    }

    @Override
    public String toString() {
        Class c = this.getClass();
        Field[] fields = c.getDeclaredFields();
        String str = "";
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                str += field.getName() + "：" + field.get(this) + "\n";
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return str;
    }
}
